<template>
  <div class="chatFoot">
    <span class="returnAdd">
      <i class="icon-add" @click="infoRelease"></i>
    </span>
    <el-input
      v-model="textarea"
      type="textarea"
      autosize
      placeholder="请输入内容"
    >
    </el-input>
    <a class="sendBtn" @click="sendVal">发送</a>
  </div>
</template>

<script>
import $ from 'jquery'
import { TrimStart, trimEnd } from '@/until/until.js'
import Chat from './client.js'

export default {
  data() {
    return {
      Chat: Chat,
      textarea: '',
      mobileSend: '',
      lastTimeCont: '',
      testCont: /<(?!br|input).*?>/g
    }
  },
  computed: {
    chatSocket() {
      return this.Chat.socket
    },
    PcTextarea() {
      return TrimStart(this.textarea, '\n') // 去除首尾换行
    },
    PhoneTextarea() {
      let phoneValCal = {}
      let arrVal = []
      let checkVal = []
      if (this.$store.state.customerPort == 'Android') {
        phoneValCal = JSON.parse(this.mobileSend.replace(/\n/g, '\\n').replace(/\r/g, '\\r'))
      } else {
        phoneValCal = this.mobileSend
      }
      phoneValCal = $.trim(phoneValCal.message.replace(this.testCont, '')).replace(/\n/g, '<br>')
      arrVal = phoneValCal.split('<br>')
      for (let index in arrVal) {
        checkVal.push($.trim(arrVal[index]))
      }
      arrVal = checkVal.join('<br>')
      return arrVal
    }
  },
  mounted() {
    window.sendVal = this.sendVal
  },
  methods: {
    sendVal(obj) {
      if (this.PcTextarea.length > 0) {
        if (this.chatSocket.readyState == WebSocket.OPEN) {
          if (this.PcTextarea != this.lastTimeCont) {
            this.replaceStr()
          }
        }
        this.lastTimeCont = this.PcTextarea
        this.textarea = '' // 发送后清空
      }
    },
    infoRelease() {
      this.$router.push({ path: '/infoRelease' })
    },
    replaceStr() {
      if (this.$store.state.customerPort == 'Android') {
        this.textarea = this.PcTextarea.replace(/\n/g, '\\n').replace(/\r/g, '\\r')
      }
      this.textarea = $.trim(this.textarea).replace(/\n/g, '<br>')
      this.chatSocket.send(this.PcTextarea)
      this.$store.commit('updatemySend', true)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
